package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.NewsTable;
import com.ysd.entity.ProductclassifiCationTable;
import com.ysd.util.DBManager;

public class ProductclassifiCationTableDaoImpl {
	public List<ProductclassifiCationTable> gatProductclassifiCationTables(){
		List<ProductclassifiCationTable> list = new ArrayList<ProductclassifiCationTable>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select * from productclassificationtable";
			st.executeQuery(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int productclassid = rs.getInt("productclassid");
				String productcategory = rs.getString("productcategory");
				ProductclassifiCationTable  productclassificationtable = new ProductclassifiCationTable(productclassid, productcategory);
				list.add(productclassificationtable);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
	//根据分类id删除产品分类
	public int deleteProductCateById(int cId) {
		return DBManager.executeUpdate("DELETE from productclassificationtable where productclassid="+cId);
	}
	//添加产品
	public int addproduct(ProductclassifiCationTable pc) {
		return DBManager.executeUpdate("INSERT into productclassificationtable VALUES(null,'"+pc.getProductcategory()+"',null)");
	}
	public List<ProductclassifiCationTable> getProductclassifiCationTable(String name){
		List<ProductclassifiCationTable> pcList = new ArrayList<ProductclassifiCationTable>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from productclassificationtable where productcategory = '"+name+"'");
			while (rs.next()) {
				int pcId = rs.getInt("productclassid");
				String pcName = rs.getString("productcategory");
				ProductclassifiCationTable pc = new ProductclassifiCationTable(pcId,pcName);
				pcList.add(pc);
			}
			return pcList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public ProductclassifiCationTable getproduct(int cateId) {
		ProductclassifiCationTable pc = null;
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs= st.executeQuery("select * from productclassificationtable where productclassid="+cateId);
			while (rs.next()) {
				int pcId = rs.getInt(1);
				String pcName = rs.getString(2);
				pc = new ProductclassifiCationTable(pcId,pcName);
			}
			return pc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int updateproduct(ProductclassifiCationTable pc) {
		return DBManager.executeUpdate("update productclassificationtable set productcategory ='"+pc.getProductcategory()+"' where productclassid="+pc.getProductclassid());
	}
	public List<ProductclassifiCationTable> getProductclassifiCationTablesByPage(int page,int size){
		List<ProductclassifiCationTable> list = new ArrayList<ProductclassifiCationTable>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "SELECT * from productclassificationtable LIMIT "+(page-1)*size+" ,"+size;
			st.executeQuery(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int productclassid = rs.getInt("productclassid");
				String productcategory = rs.getString("productcategory");
				ProductclassifiCationTable  productclassificationtable = new ProductclassifiCationTable(productclassid, productcategory);
				list.add(productclassificationtable);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
}
